const chart = klinecharts.init('main');
let chart_type = 0; // 图表类型标志
const history_url = "/kline-history";
const polling_url = "/kline-polling";
class Deedfeeds {

    constructor() {
        this.currentInterval = null
        this.subscribeDataParam = {
            interval: null,
            setHistoryData: null,
            subscribeData: null
        } // 再次订阅时需要的数据
    }

    getHistory(interval) {
       const  historyData = []
        $.ajax({
            url: history_url,
            type: "POST",
            data: { interval: interval, symbol: window.SYMBOL},
            async: false, // 将 async 设置为 false 以使请求同步
            success: function(response) {
              if (response.code!==0) {
                  $.msg.error(response.msg);
                  return;
              }
                response.data.forEach(item => {
                    historyData.unshift({
                        time: item.id * 1000, // 时间
                        open: item.open, // 开
                        high: item.high, // 高
                        low: item.low, // 低
                        close: item.close, // 收
                        volume: item.vol // 交易量
                    })
                })
                chart.applyNewData(historyData.reverse())
            }
        },"json")
        this.polling();
       var _this = this;
        setInterval(function (){
            _this.polling();
        },2000)

    }

    polling() {
        var _this = this;
        $.ajax({
            url: polling_url,
            type: "POST",
            data: { symbol: window.SYMBOL},
            async: false, // 将 async 设置为 false 以使请求同步
            success: function(response) {
                if (response.code!==0) {
                    $.msg.error(response.msg);
                    return;
                }
                const perData = response.data;
                const perData1 = {
                    open: perData.open, // 开盘价，必要字段
                    close: perData.close, // 收盘价，必要字段
                    high: perData.high, // 最高价，必要字段
                    low: perData.low, // 最低价，必要字段
                    volume: perData.vol, // 成交量，非必须字段
                    timestamp: perData.id * 1000 // 时间戳，毫秒级别，必要字段
                };
                chart.updateData(perData1);
                _this.subscribeDataParam.subscribeData({
                    time: perData.id * 1000, // 时间
                    open: perData.open, // 开
                    high: perData.high, // 高
                    low: perData.low, // 低
                    close: perData.close, // 收
                    volume: perData.vol // 交易量
                });
            }
        },"json")
    }

    setHistoryData({ interval, setHistoryData, subscribeData }) {
        console.log(interval, setHistoryData, subscribeData)
        this.currentInterval = interval
        this.subscribeDataParam = {
            interval,
            setHistoryData,
            subscribeData
        }
        console.log(interval)
        this.getHistory(interval)
    }

    intervalChanged({ interval, setHistoryData, subscribeData }) {
        this.subscribeDataParam = {
            interval,
            setHistoryData,
            subscribeData
        }
        this.currentInterval = interval;
    }
}

window.deedfeeds = new Deedfeeds();